<?php

declare(strict_types=1);

namespace App\Model\Act;

use Hyperf\DbConnection\Db;
use Hyperf\DbConnection\Model\Model;

/**
 * 许愿树活动
 */
class WishTree extends Model
{
    protected $table = 'act_wish_tree';

    const WISH_COIN = [
        ['times' => 1, 'coin' => 20],
        ['times' => 10, 'coin' => 200],
        ['times' => 100, 'coin' => 2000],
    ];

    static function getCostCoin($wishTimes = 1)
    {
        foreach (self::WISH_COIN as $conf) {
            if ($wishTimes == $conf['times']) return $conf['coin'];
        }
        return 0;
    }

    static function addRecord($userId = 0, $wishGiftId = 1, $giftNum = 1, $coin = 0)
    {
        $model = new self();
        $model->user_id = $userId;
        $model->wish_gift_id = $wishGiftId;
        $model->num = $giftNum;
        $model->coin = $coin;
        $model->flag = 1;
        $model->saveOrFail();
    }

    /**
     * 获取列表
     */
    static function getList($num = 0, $userId = 0)
    {
        $where = ['flag' => 1];
        if ($userId) $where['user_id'] = $userId;
        return self::where($where)->orderBy('coin', 'desc')->limit($num)->get()->toArray();
    }

    static function getRankList()
    {
        $items = self::where('flag', 1)->select(['user_id', 'num'])->get()->toArray();
        $data = [];
        foreach ($items as $item) {
            if (!isset($data[$item['user_id']])) $data[$item['user_id']] = $item['num'];
            else $data[$item['user_id']] += $item['num'];
        }
        arsort($data);
        return $data;
    }

    /**
     * 当天已消耗完的礼物数
     */
    static function getRemainGifts($giftConf = [], $start = '')
    {
        $start = $start ?: date('Y-m-d');
        $where = [
            ['created_at', '>', $start],
            ['flag', 1]
        ];
        $data = self::where($where)->select(['wish_gift_id', Db::raw('SUM(num) as num')])->groupBy('wish_gift_id')->get()->toArray();
        $remainGifts = [];
        foreach ($data as $item) {
            $giftId = $item['wish_gift_id'];
            $remainGifts[$giftId] = $giftConf[$giftId]['gift_num'] - $item['num'];
        }
        foreach ($giftConf as $giftItem) {
            $remainGifts[$giftItem['gift_id']] = $remainGifts[$giftItem['gift_id']] ?? $giftItem['gift_num'];
        }
        return $remainGifts;
    }

    /**
     * 礼物领取数量
     */
    static function getGiftNum($giftId = 1, $start = '', $num = 100, $userId = 0)
    {
        $where = [
            ['flag', 1]
        ];
        $giftId && $where[] = ['wish_gift_id', $giftId];
        $start && $where[] = ['created_at', '>', $start];
        $data = self::where($where)->select(['user_id', Db::raw('SUM(num) as num')])->groupBy('user_id')->orderBy('num', 'desc')->get()->toArray();
        $myRank = $myNum = 0;
        foreach ($data as $k => $item) {
            if ($item['user_id'] == $userId) {
                $myRank = $k + 1;
                $myNum = $item['num'];
                break;
            }
        }
        return ['rank_list' => array_slice($data, 0, $num), 'my_rank' => $myRank, 'my_num' => $myNum];
    }
}
